package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.entity.Problem;
import org.easy.tool.entity.User;
import org.easy.tool.entity.UserProblem;
import org.easy.tool.mapper.UserProblemMapper;
import org.easy.tool.service.IProblem;
import org.easy.tool.service.IUser;
import org.easy.tool.service.IUserProblem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Valley
 * @since 2023-08-25 14:20:43
 */
@Service
public class UserProblemService extends ServiceImpl<UserProblemMapper, UserProblem> implements IUserProblem {

    @Autowired
    UserProblemMapper userProblemMapper;

    @Autowired
    IUser userService;
    @Autowired
    IProblem problemService;

    /**
     * 功能：根据用户id和题目id查询当前关联类
     */
    @Override
    public UserProblem getUserProblemById(String userId, String problemId) {
        LambdaQueryWrapper<UserProblem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserProblem::getUserId, userId);
        wrapper.eq(UserProblem::getProblemId, problemId);
        return getOne(wrapper);
    }

    /**
     * 功能：新增用户题目关联
     */
    @Override
    public String addUserProblem(UserProblem userProblem) {
        save(userProblem);
        return userProblem.getId();
    }

    /**
     * 功能：获取当前用户所有的题目
     */
    @Override
    public List<UserProblem> getUserProblemList(String userId) {
        LambdaQueryWrapper<UserProblem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserProblem::getUserId, userId);
        return userProblemMapper.selectList(wrapper);
    }


    /**
     * 功能：移除当前用户题目
     */
    @Override
    public void removeUserProblem(String userId, String problemId) {
        UserProblem userProblemById = getUserProblemById(userId, problemId);
        userProblemById.setDeleted(1);
        updateById(userProblemById);
    }

    /**
     * 功能：某个用户题目总计数量
     */
    @Override
    public int countTotal(String userId, String problemId) {
        return countRight(userId, problemId) + countWrong(userId, problemId);
    }

    /**
     * 功能：某个用户题目正确数量
     */
    @Override
    public int countRight(String userId, String problemId) {
        return getUserProblemById(userId, problemId).getRight();
    }


    /**
     * 功能：某个用户题目错误数量
     */
    @Override
    public int countWrong(String userId, String problemId) {
        return getUserProblemById(userId, problemId).getWrong();
    }

    /**
     * 功能：获取用户全部答题数量
     */
    @Override
    public int countTotal(String userId) {
        return countRight(userId) + countWrong(userId);
    }

    /**
     * 功能：用户正确总数
     */
    @Override
    public int countRight(String userId) {
        int count = 0;
        for (UserProblem userProblem : getUserProblemList(userId)) {
            count += countRight(userId, userProblem.getProblemId());
        }
        return count;
    }

    /**
     * 功能：用户错误总数
     */
    @Override
    public int countWrong(String userId) {
        int count = 0;
        for (UserProblem userProblem : getUserProblemList(userId)) {
            count += countWrong(userId, userProblem.getProblemId());
        }
        return count;
    }

    /**
     * 功能：随机抽取一个用户题目
     */
    @Override
    public Problem randomUserProblem(String userId) {
        return null;
    }

    /**
     * 功能：用户做某道题的正确率
     */
    @Override
    public double getUserProblemRightRate(String userId, String problemId) {
        double right = countRight(userId, problemId);
        double total = countTotal(userId, problemId);
        return right/total;
    }


}
